<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let input=`6
        -1 -1 -6 7 -17 7 
        2`
        let spl=input.split('\n')
        let [len,k]=[parseInt(spl[0].trim()),parseInt(spl[2].trim())]
        let score=spl[1].trim().split(' ').map(v=>parseInt(v))
        let i=0
        let res=0

        while(i<len){
            let list=score.slice(i,i+k)
            let max=Math.max(...list)
            if(max<=0){
                res+=max
                i+=list.length-list.reverse().findIndex(v=>v===max)          
            }else{
                let list_z=list.filter((v)=>v>0)
                let min=Math.min(...list_z)           
                res+=min
                i+=list.findIndex(v=>v===min)+1
            }


        }

        console.log([len,k],score);
        console.log(res);
        // console.log(score.reverse());
        
        
        
       
    </script>
</body>
</html>